//
//  MyCarFavoriteView.m
//  SecondPage
//
//  Created by xiong qi on 13-4-19.
//  Copyright (c) 2013年 winston. All rights reserved.
//

#import "MyCarFavoriteView.h"
#import "MyCarSQLHelp.h"
#import "MyCarPrintView.h"
#import "../../ResourceLoader/ResourceLoader/ResourceLoader.h"


@implementation MyCarFavoriteView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        UIImage *img=[UIImage imageNamed:@"AllAuto_Comparison_Image_BG"];
        img=[img stretchableImageWithLeftCapWidth:15 topCapHeight:12];
        
        backGroundView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 656, 505)];
        [self addSubview:backGroundView];
        backGroundView.clipsToBounds = NO;
        backGroundView.userInteractionEnabled = YES;
        [backGroundView release];
        [backGroundView setImage:img];
        
        
        contentView = [[UIScrollView alloc] initWithFrame:CGRectMake(38, 20, 590, 464)];
        [backGroundView addSubview:contentView];
        [contentView release];
        
        
        panoramaView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 228, 171)];
        [contentView addSubview:panoramaView];
        [panoramaView release];
        panoramaView .contentMode = UIViewContentModeScaleAspectFit;
        
        // @chenms @0823
        float factor = 656.0 / 846.0;// 和打印界面尺寸的比值
        companyimageview = [[UIImageView alloc] initWithFrame:CGRectMake(20 * factor, -5.0 * factor, 114 * factor, 30 * factor)];
        [contentView addSubview:companyimageview];
        [companyimageview release];
        
        itemView1 = [[MyCarItemView alloc] initWithFrame:CGRectMake(270, 0, 150, 124)];
        [contentView addSubview:itemView1];
        [itemView1 release];
        
        itemView2 = [[MyCarItemView alloc] initWithFrame:CGRectMake(270+150+15, 0, 150, 124)];
        [contentView addSubview:itemView2];
        [itemView2 release];
        
        itemView3 = [[MyCarItemView alloc] initWithFrame:CGRectMake(270, 124+20, 150, 124)];
        [contentView addSubview:itemView3];
        [itemView3 release];
        
        itemView4 = [[MyCarItemView alloc] initWithFrame:CGRectMake(270+150+15, 124+20, 150, 124)];
        [contentView addSubview:itemView4];
        [itemView4 release];
        
        
        paraView = [[MyCarParameterView alloc] initWithFrame:CGRectMake(10, 181, 218+10, 290)];
        [contentView addSubview:paraView];
        [paraView release];
        
        inputView = [[MyCarUserInputView alloc] initWithFrame:CGRectMake(270, 298, 315, 160)];
        [contentView addSubview:inputView];
        [inputView release];
        inputView.parentView = self;
        
        
        btnSave = [[UIButton alloc] initWithFrame:CGRectMake(74, 516, 155, 44)];
        [self addSubview:btnSave];
        [btnSave release];
        [btnSave setBackgroundImage:[UIImage imageNamed:@"AllAuto_Config_Button_Unselect"] forState:UIControlStateNormal];
        [btnSave setBackgroundImage:[UIImage imageNamed:@"AllAuto_Config_Button_Select"] forState:UIControlStateHighlighted];
        [btnSave setTitle:@"保 存" forState:UIControlStateNormal];
        [btnSave setTitleColor:[UIColor colorWithRed:0.4 green:0.4 blue:0.4 alpha:1.0] forState:UIControlStateNormal];
        [btnSave setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
        [btnSave addTarget:self action:@selector(onSaveClick:) forControlEvents:UIControlEventTouchUpInside];
        
        btnPrint = [[UIButton alloc] initWithFrame:CGRectMake(246, 516, 155, 44)];
        [self addSubview:btnPrint];
        [btnPrint release];
        [btnPrint setBackgroundImage:[UIImage imageNamed:@"AllAuto_Config_Button_Unselect"] forState:UIControlStateNormal];
        [btnPrint setBackgroundImage:[UIImage imageNamed:@"AllAuto_Config_Button_Select"] forState:UIControlStateHighlighted];
        [btnPrint setTitle:@"打印预览" forState:UIControlStateNormal];
        [btnPrint setTitleColor:[UIColor colorWithRed:0.4 green:0.4 blue:0.4 alpha:1.0] forState:UIControlStateNormal];
        [btnPrint setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
        [btnPrint addTarget:self action:@selector(onPrint:) forControlEvents:UIControlEventTouchUpInside];
        
        btnSend = [[UIButton alloc] initWithFrame:CGRectMake(418, 516, 155, 44)];
        [self addSubview:btnSend];
        [btnSend release];
        [btnSend setBackgroundImage:[UIImage imageNamed:@"AllAuto_Config_Button_Unselect"] forState:UIControlStateNormal];
        [btnSend setBackgroundImage:[UIImage imageNamed:@"AllAuto_Config_Button_Select"] forState:UIControlStateHighlighted];
        [btnSend setTitle:@"发送预览" forState:UIControlStateNormal];
        [btnSend setTitleColor:[UIColor colorWithRed:0.4 green:0.4 blue:0.4 alpha:1.0] forState:UIControlStateNormal];
        [btnSend setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
        [btnSend addTarget:self action:@selector(onSend:) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

-(void)RefreshPanoramaImage:(NSString *)imagepath
{
    UIImage * image = [[UIImage alloc] initWithContentsOfFile:imagepath];
    if (image) {
        panoramaView.image = image;
        [image release];
    }
}

-(void)RefreshByOtherData:(NSArray *)array
{
    int count = [array count];
    if (count != 5) {
        return;
    }
    
    
    for (int i=0; i<count; i++) {
        
        switch (i) {
            case 0:
                [itemView1 RefreshImageData:[array objectAtIndex:i]];
                break;
            case 1:
                [itemView2 RefreshImageData:[array objectAtIndex:i]];
                break;
            case 2:
                [paraView RefreshParameterData:[array objectAtIndex:i]];
                [inputView RefreshCarType:[array objectAtIndex:i]];
                break;
            case 3:
                [itemView3 RefreshImageData:[array objectAtIndex:i]];
                break;
            case 4:
                [itemView4 RefreshImageData:[array objectAtIndex:i]];
                break;
                
            default:
                break;
        }
    }
    
    // @chenms @0906
    int isNotHonda = [[paraView.dataSource objectForKey:CarInfo_IsNotHonda] intValue];
    companyimageview.image = [UIImage imageNamed:isNotHonda ? ResourceLoader_CarExtra_CompanyLogoNameSpecial : ResourceLoader_CarExtra_CompanyLogoNameNormal];
}


-(void)ScrollViewToTop:(NSNumber *)num
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:0.3f];
    
    contentView.contentOffset = CGPointMake(0, [num intValue]);
    
    [UIView commitAnimations];
}



-(void)onSaveClick:(UIButton *)sender
{
    NSDictionary* cardic = @{@"carcolor": [itemView3 GetCode],
                                @"insidecolor": [itemView4 GetCode]};
    [inputView SendSaveData:cardic];
}

-(void)onPrint:(UIButton *)sender
{
//    [inputView SendSaveData];
    
    MyCarPrintView * printView = [[MyCarPrintView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768) functionBtnTitle:@"打 印"];
    printView.autoID = self.autoID;
    [printView SetCarImage:panoramaView.image];
    [printView SetCarConfigImage:itemView1.dataSource Index:1];
    [printView SetCarConfigImage:itemView2.dataSource Index:2];
    [printView SetCarConfigImage:itemView3.dataSource Index:3];
    [printView SetCarConfigImage:itemView4.dataSource Index:4];
    [printView SetCarInfoImage:paraView.dataSource];
    
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    [window.rootViewController.view addSubview:printView];
    [printView release];
}

-(void)onSend:(UIButton *)sender
{
    if (nil == inputView.usermail.text || 0 == inputView.usermail.text.length) {
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:nil message:@"邮箱不能为空" delegate:nil cancelButtonTitle:@"我知道了" otherButtonTitles:nil];
        [alert show];
        SafeRelease(alert);
        
        return;
    }
    
    MyCarPrintView * printView = [[MyCarPrintView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768) functionBtnTitle:@"发送邮件"];
    printView.userEmail = inputView.usermail.text;
    printView.userName  = inputView.username.text;
    printView.autoID = self.autoID;
    [printView SetCarImage:panoramaView.image];
    [printView SetCarConfigImage:itemView1.dataSource Index:1];
    [printView SetCarConfigImage:itemView2.dataSource Index:2];
    [printView SetCarConfigImage:itemView3.dataSource Index:3];
    [printView SetCarConfigImage:itemView4.dataSource Index:4];
    [printView SetCarInfoImage:paraView.dataSource];
    
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    [window.rootViewController.view addSubview:printView];
    [printView release];
    
}

-(void)dealloc
{
    [super dealloc];
}
@end
